//
//  MeansViewController.m
//  Article
//
//  Created by LONG on 16/5/7.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MeansViewController.h"
#import "MeansTableViewCell.h"

@interface MeansViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *headerview;
@property (weak, nonatomic) IBOutlet UIImageView *headerTitle;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic,strong)NSMutableArray *dataarr;

@end

@implementation MeansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.userInteractionEnabled = NO;
    [self requestTableview];
    [self setExtraCellLineHidden:_tableview];
    
}
-(UIImage *) getImageFromURL:(NSString *)fileURL {
    //NSLog(@"执行图片下载函数");
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    return result;
}
- (void)requestTableview{
    if (_dataarr == nil ) {
        _dataarr = [NSMutableArray arrayWithCapacity:0];
        
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                          @"uid":_uid};
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:EMPLOYEE_INFO_URL parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
           
            //追加数据
            [self.dataarr addObject:[responseObject objectForKey:@"data"]];
            self.headerview.image = [self blurryImage:[self getImageFromURL:[[responseObject objectForKey:@"data"] objectForKey:@"avatar512"]] withBlurLevel:10];
            [self.headerTitle sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[responseObject objectForKey:@"data"] objectForKey:@"avatar128"]]]];
            self.titleLab.text = [[responseObject objectForKey:@"data"] objectForKey:@"nickname"];
            
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        
        [_tableview reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}
- (UIImage *)blurryImage:(UIImage *)image
           withBlurLevel:(CGFloat)blur {
    CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"
                                  keysAndValues:kCIInputImageKey, inputImage,
                        @"inputRadius", @(blur),
                        nil];
    
    CIImage *outputImage = filter.outputImage;
    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRef outImage = [context createCGImage:outputImage
                                             fromRect:[outputImage extent]];
    return [UIImage imageWithCGImage:outImage];
}



#pragma mark tableView代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MeansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MeansTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSArray *namearr = @[@"昵称:",@"真实姓名:",@"手机号:",@"加入时间:",@"最近登录:",@"登录次数:",@"备注:"];
    NSArray *keyarr = @[@"nickname",@"truename",@"mobile",@"reg_time",@"last_login_time",@"login",@"remark"];

    cell.imagelab.text = namearr[indexPath.row];
    if (_dataarr.count == 0) {
        return cell;
    }
    if (indexPath.row == 6) {
        cell.lable.text = [NSString stringWithFormat:@"%@",_dataarr[0][@"userlink"][@"remark"]];
    }else{
        cell.lable.text = [NSString stringWithFormat:@"%@",_dataarr[0][keyarr[indexPath.row]]];
    }
    return cell;

}

#pragma mark delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 35;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
